package no.hio.wsprovider.services;

import static org.junit.Assert.*;
import static org.easymock.EasyMock.*;
import java.util.List;
import java.util.ArrayList;
import java.util.NoSuchElementException;

import no.hio.wsprovider.dao.ProductDAO;
import no.hio.wsprovider.vo.Product;

import org.junit.*;


public class ProductServiceImplEasyMockTestCase {

	ProductServiceImpl service;
	ProductDAO mock;
	Product testProd;
	
	@Before
	public void setUp()
	{
		service = new ProductServiceImpl();
		mock = createMock(ProductDAO.class);
		
		testProd = new Product();
		testProd.setProductID(1);
		
		service.setProductDAO(mock);
	}
	
	@Test
	public void testInsertProduct() {
		//set expected method call and return value
		
		
		expect(mock.insert(testProd)).andReturn(1);
		
		//start "real" behaviour
		replay(mock);
		
		//do assertion
		assertEquals(1, service.insertProduct(testProd));
		//verify that method is called.
		verify(mock);
		
		
		
		//reset the mock
		reset(mock);
		
		//set expected method call and exception.
		expect(mock.insert(testProd)).andThrow(new UnsupportedOperationException());
		
		replay(mock);
		
		assertEquals(-1, service.insertProduct(testProd));
		
		verify(mock);
		
		
	}

	@Test
	public void testUpdateProduct() {
		
		mock.update(testProd);
		replay(mock);
		
		service.updateProduct(testProd);
		
		verify(mock);
	}

	@Test
	public void testFindAllProducts() {
		
		expect(mock.findAll()).andReturn((new ArrayList<Product>()));
		replay(mock);
		
		List<Product> prodList = service.findAllProducts();
		assertTrue(prodList.isEmpty());
		verify(mock);
	}

	@Test
	public void testFindByProductID() {
		expect(mock.findByProductID(1)).andReturn(testProd);
		replay(mock);
		
		assertEquals(testProd, service.findByProductID(1));
		verify(mock);
	}

	@Test
	public void testRemoveProduct() {
		mock.remove(testProd);
		replay(mock);
		service.removeProduct(testProd);
		verify(mock);
		reset(mock);
		
		mock.remove(testProd);
		expectLastCall().andThrow(new NoSuchElementException());
		replay(mock);
		service.removeProduct(testProd);
		verify(mock);
		
	}

}
